package gobang_util;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.regex.Pattern;

import com.woniuxy.gobang.ui.Global;
import com.woniuxy.gobang.ui.Piece;

import javafx.scene.paint.Color;
import messge.Message;

public class GobangUtil {
	// 发送消息工具方法
	public static void sendMessage(Message message) {
		try (Socket socket = new Socket(Global.otherIP, Global.otherPort);
				ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());) {
			oos.writeObject(message);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param ip
	 * @return ip格式正确返回true
	 */
	public static boolean ipCheck(String ip) {
		String ipReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
		Pattern ipPattern = Pattern.compile(ipReg);

		if (ipPattern.matcher(ip).matches() || ip.equals("localhost")) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @return 如果端口格式正确返回true
	 */
	public static boolean portCheck(int port) {

		if (port >= 0 && port <= 65535) {
			return true;
		}

		return false;
	}
}
